Back to course home
0% completed
Vote For New Content
Microsoft Medium Problems
Nth Digit (medium)
Solution: Nth Digit
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Find Original Array From Doubled Array (medium)
Solution: Find Original Array From Doubled Array
Reverse Words in a String II (medium)
Solution: Reverse Words in a String II
Longest Arithmetic Subsequence (medium)
Solution: Longest Arithmetic Subsequence
Matchsticks to Square (medium)
Solution: Matchsticks to Square
Score of Parentheses (medium)
Solution: Score of Parentheses
Maximum Length of a Concatenated String with Unique Characters (medium)
Solution: Maximum Length of a Concatenated String with Unique Characters
Inorder Successor in BST (medium)
Solution: Inorder Successor in BST
Distribute Coins in Binary Tree (medium)
Solution: Distribute Coins in Binary Tree
Spiral Matrix (medium)
Solution: Spiral Matrix
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Rotate Image (medium)
Solution: Rotate Image
Combinations (medium)
Solution: Combinations
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Multiply Strings (medium)
Solution: Multiply Strings
Best Time to Buy and Sell Stock II (medium)
Solution: Best Time to Buy and Sell Stock II
Largest Palindromic Number (medium)
Solution: Largest Palindromic Number
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Next Permutation (medium)
Solution: Next Permutation
132 Pattern (medium)
Solution: 132 Pattern
Valid Sudoku (medium)
Solution: Valid Sudoku
Number of Islands (medium)
Solution: Number of Islands
Buildings With an Ocean View (medium)
Solution: Buildings With an Ocean View
Microsoft Hard Problems
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Can Place Flowers (easy)
Problem Statement
You have a long, narrow flowerbed divided into sections, each of which can either be empty or filled with a flower. Due to gardening restrictions, you cannot plant flowers in adjacent sections — they must be at least one section apart to prevent overcrowding.
Given the current state of the flowerbed (represented as an array, where 0 indicates an empty section and 1 signifies a section with a flower) and a number representing how many more flowers you wish to plant, determine if you can plant all the desired flowers without breaking the gardening restrictions.
Examples
-
Example 1:
- Input: flowerbed =
[0,0,1,0,1,0,0], n =2 - Expected Output: true
- Justification: You can plant flowers in the 0th and 6th index, satisfying the non-adjacent rule.
- Input: flowerbed =
-
Example 2:
- Input: flowerbed =
[1,0,0,0,0,1], n =2 - Expected Output: false
- Justification: Despite the four consecutive empty sections, you can only plant one flower (either in the 2nd or 3rd index) without violating the rule of not planting in adjacent sections.
- Input: flowerbed =
-
Example 3:
- Input: flowerbed =
[0,0,1,0,0], n =1 - Expected Output: true
- Justification: You can plant one flower either in the 0th or 4th index, adhering to the non-adjacent rule.
- Input: flowerbed =
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Buddy Strings
Solution: Can Place Flowers
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself